<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>how-to/c/listChannels.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('how-to_2c_2list_channels_8c-example.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">how-to/c/listChannels.c</div>  </div>
</div><!--header-->
<div class="contents">
<p>This example shows how to show information about all enumerated CANlib channels. See <a class="el" href="page_user_guide_init.html#section_user_guide_init_lib_init">Library Initialization</a> and <a class="el" href="page_user_guide_device_and_channel.html#section_user_guide_identifying_devices">Identifying Devices and Channels</a> for more information.</p>
<p>------------------------------------------------------------------------------</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;stdint.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="canlib_8h.html">canlib.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat);</div><div class="line"><span class="keywordtype">void</span> show_channel_info(<span class="keywordtype">int</span> channel_no);</div><div class="line"></div><div class="line"><span class="comment">// Find out how many channels that exists and loop over them</span></div><div class="line"><span class="keywordtype">void</span> list_channels(<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  <span class="keywordtype">char</span> driverName[256];</div><div class="line">  <span class="keywordtype">int</span> chanCount = 0;</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">  <span class="keywordtype">int</span> i;</div><div class="line"></div><div class="line">  stat = <a name="a0"></a><a class="code" href="group__can__general.html#ga65169ca633cd30aa92b8a80e28a5378b">canGetNumberOfChannels</a>(&amp;chanCount);</div><div class="line">  check(<span class="stringliteral">&quot;canGetNumberOfChannels&quot;</span>, stat);</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;Found %d channel(s).\n&quot;</span>, chanCount);</div><div class="line">  <span class="keywordflow">for</span> (i = 0; i &lt; chanCount; i++) {</div><div class="line">    show_channel_info(i);</div><div class="line">  }</div><div class="line">} <span class="comment">// list_channels</span></div><div class="line"></div><div class="line"><span class="comment">// Print status message if stat contains an error code</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (stat != <a name="a1"></a><a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">    <span class="keywordtype">char</span> buf[50];</div><div class="line">    buf[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">    <span class="comment">// Retreive informational text about the status code</span></div><div class="line">    <a name="a2"></a><a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a>(stat, buf, <span class="keyword">sizeof</span>(buf));</div><div class="line">    printf(<span class="stringliteral">&quot;%s: failed, stat=%d (%s)\n&quot;</span>, <span class="keywordtype">id</span>, (<span class="keywordtype">int</span>)stat, buf);</div><div class="line">  }</div><div class="line">} <span class="comment">// check</span></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> show_channel_info(<span class="keywordtype">int</span> channel_no)</div><div class="line">{</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">  <span class="keywordtype">char</span> name[256];</div><div class="line">  <span class="keywordtype">char</span> driverName[256];</div><div class="line">  <span class="keywordtype">char</span> custChanName[40];</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> ean[2], fw[2], serial[2];</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> chan_no_on_card;</div><div class="line"></div><div class="line"></div><div class="line">  memset(name, 0, <span class="keyword">sizeof</span>(name));</div><div class="line">  memset(custChanName, 0, <span class="keyword">sizeof</span>(custChanName));</div><div class="line">  memset(ean, 0, <span class="keyword">sizeof</span>(ean));</div><div class="line">  memset(fw, 0, <span class="keyword">sizeof</span>(fw));</div><div class="line">  memset(serial, 0, <span class="keyword">sizeof</span>(serial));</div><div class="line"></div><div class="line">  stat = <a name="a3"></a><a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel_no, <a name="a4"></a><a class="code" href="canlib_8h.html#afc7edc82e3d370444c3b83c66a9c6479">canCHANNELDATA_DEVDESCR_ASCII</a>, &amp;name, <span class="keyword">sizeof</span>(name));</div><div class="line">  check(<span class="stringliteral">&quot;canGetChannelData: DEVDESCR_ASCII&quot;</span>, stat);</div><div class="line"></div><div class="line">  stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel_no, <a name="a5"></a><a class="code" href="canlib_8h.html#a9062beaa59d9787667bac6cbbc6b4e20">canCHANNELDATA_CARD_UPC_NO</a>, &amp;ean, <span class="keyword">sizeof</span>(ean));</div><div class="line">  check(<span class="stringliteral">&quot;canGetChannelData: CARD_UPC_NO&quot;</span>, stat);</div><div class="line"></div><div class="line">  stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel_no, <a name="a6"></a><a class="code" href="canlib_8h.html#a8b3b4903a766565c95afd7c90f76d994">canCHANNELDATA_CARD_SERIAL_NO</a>, &amp;serial, <span class="keyword">sizeof</span>(serial));</div><div class="line">  check(<span class="stringliteral">&quot;canGetChannelData: CARD_SERIAL_NO&quot;</span>, stat);</div><div class="line"></div><div class="line">  stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel_no, <a name="a7"></a><a class="code" href="canlib_8h.html#a1ab7e59c0771f2f9850e8fe1d2d58ff1">canCHANNELDATA_CARD_FIRMWARE_REV</a>, &amp;fw, <span class="keyword">sizeof</span>(fw));</div><div class="line">  check(<span class="stringliteral">&quot;canGetChannelData: CARD_FIRMWARE_REV&quot;</span>, stat);</div><div class="line"></div><div class="line">  stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel_no, <a name="a8"></a><a class="code" href="canlib_8h.html#a4e0823a649d26191cde50f2c4fa5a65d">canCHANNELDATA_CHAN_NO_ON_CARD</a>,</div><div class="line">                           &amp;chan_no_on_card, <span class="keyword">sizeof</span>(chan_no_on_card));</div><div class="line">  check(<span class="stringliteral">&quot;canGetChannelData: canCHANNELDATA_CHAN_NO_ON_CARD&quot;</span>, stat);</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel_no, <a name="a9"></a><a class="code" href="canlib_8h.html#a0e91dd0d999b3043c6da5cb911dd83d8">canCHANNELDATA_CUST_CHANNEL_NAME</a>,</div><div class="line">                    custChanName, <span class="keyword">sizeof</span>(custChanName));</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;ch %2.1d: %-22s\t%x-%05x-%05x-%x, s/n %u, v%u.%u.%u :%d \&quot;%s\&quot;\n&quot;</span>,</div><div class="line">         channel_no, name,</div><div class="line">         (ean[1] &gt;&gt; 12), ((ean[1] &amp; 0xfff) &lt;&lt; 8) | ((ean[0] &gt;&gt; 24) &amp; 0xff),</div><div class="line">         (ean[0] &gt;&gt; 4) &amp; 0xfffff, (ean[0] &amp; 0x0f),</div><div class="line">         serial[0],</div><div class="line">         fw[1] &gt;&gt; 16, fw[1] &amp; 0xffff, fw[0] &amp; 0xffff,</div><div class="line">         chan_no_on_card, custChanName);</div><div class="line">} <span class="comment">// list_channel</span></div><div class="line"></div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
